package com.example.android.ouactivity.login_register;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.android.ouactivity.MainActivity;
import com.example.android.ouactivity.R;
import com.example.android.ouactivity.model.LoginBean;
import com.example.android.ouactivity.utils.HttpUtils;
import com.example.android.ouactivity.utils.ReadUserInfo;
import com.google.gson.Gson;

public class RegisterActivity extends AppCompatActivity {
    private EditText nameView,passView;
    private Button submit;
    private String name,password,postString;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = msg.obj.toString();
            Gson gson = new Gson();
            LoginBean register = gson.fromJson(result,LoginBean.class);
            ReadUserInfo readUserInfo = new ReadUserInfo(RegisterActivity.this,"userInfo");
            if(register.getCode()==1){
                String info = register.getUser()+","+name+","+password;
                readUserInfo.saveInfo(info);
                Toast.makeText(RegisterActivity.this,register.getMessage(),Toast.LENGTH_SHORT).show();
                if(readUserInfo.hasFile()){
                    Log.i("user:",readUserInfo.getInfo());
                }
                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                startActivity(intent);
            }else {
                Toast.makeText(RegisterActivity.this,register.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        //设置返回，及页面标题
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        if (actionBar != null) {
            actionBar.setTitle("评论");
        }
        nameView = (EditText)findViewById(R.id.Register_name);
        passView = (EditText)findViewById(R.id.Register_pass);
        submit = (Button)findViewById(R.id.Register_submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name = nameView.getText().toString();
                password = Login.md5(passView.getText().toString());
                if(!name.equals("") && !password.equals("")){
                    postString = "username="+name+"&password="+password;
                    HttpUtils httpUtils = new HttpUtils();
                    httpUtils.getWebJsonByPost("http://www.chqing.cn/register",handler,postString);
                }
            }
        });

    }
    //左上角返回的监听
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
